﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class agent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.imgbtnLogin.Click += new ImageClickEventHandler(imgbtnLogin_Click);
        this.lnkbtnLogout.Click += new EventHandler(lnkbtnLogout_Click);
        if (!IsPostBack)
        {
            this.menu.Logo = "1";

            SetLoginStatus();

            InitFileList();
        }
    }

    void lnkbtnLogout_Click(object sender, EventArgs e)
    {
        Session["name"] = null;
        SetLoginStatus();

        InitFileList();
    }

    void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
    {
        CAgent agent = new CAgent();
        if (agent.Login(this.tbName.Text,this.tbPwd.Text))
        {
            Session["name"] = this.tbName.Text;            
        }
        else
        {
            Session["name"] = null;            
        }
        SetLoginStatus();
        InitFileList();
    }

    private void InitFileList()
    {
        CFile file = new CFile();
        DataTable dt = file.ReadFiles(1);
        this.ltlfile1.Text = GetLinks(dt);

        dt = file.ReadFiles(2);
        this.ltlfile2.Text = GetLinks(dt);

        dt = file.ReadFiles(3);
        this.ltlfile3.Text = GetLinks(dt);

        dt = file.ReadFiles(4);
        this.ltlfile4.Text = GetLinks(dt);

        dt = file.ReadFiles(5);
        this.ltlfile5.Text = GetLinks(dt);
    }

    private string GetLinks(DataTable dt)
    {
        if (null != dt)
        {
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<dt.Rows.Count; i++)
            {
                sb.Append("<dd>");
                if (null == Session["name"])
                {
                    sb.Append((i + 1).ToString() + "." + dt.Rows[i]["title"].ToString());
                }
                else
                {
                    sb.Append("<a href=\"" + dt.Rows[i]["path"].ToString() + "\">" + (i + 1).ToString() + "." + dt.Rows[i]["title"].ToString() + "</a>");
                }
                sb.Append("</dd>");
            }
            return sb.ToString();
        }
        return null;
    }

    private void SetLoginStatus()
    {
        if (null != Session["name"])
        {
            this.ltlLoginTitle.Text = Session["name"].ToString() + "，欢迎您!"; ;
            this.pnlLogin.Visible = false;
            this.lnkbtnLogout.Visible = true;
        }
        else
        {
            this.tbPwd.Text = "";
            this.ltlLoginTitle.Text = "用户登录";
            this.pnlLogin.Visible = true;
            this.lnkbtnLogout.Visible = false;
        }
    }
}